//
//  ZWelcomeViewController.swift
//  swift-weibo
//
//  Created by zero on 17/3/23.
//  Copyright © 2017年 zero. All rights reserved.
//

import UIKit

import SnapKit
import SDWebImage

private let bottom:CGFloat = 180

private let button:CGFloat = 85

class ZWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        view.backgroundColor = UIColor.white
        
        setupUI()
    }

    //  布局
    private func setupUI(){
        
        view.addSubview(imageView)
        
        //  约束
        imageView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-bottom)
            
            make.size.equalTo(CGSize(width: button, height: button))
            
        }
        
        
        imageView .sd_setImage(with: ZUserAccountViewModel.shareUserAccoutn.headURL, placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
        
        view.addSubview(titleLabel)
        
        //  约束
        titleLabel.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(imageView)
            make.top.equalTo(imageView.snp.bottom).offset(16)
        }
        
        titleLabel.preferredMaxLayoutWidth = 200
        
        titleLabel.numberOfLines = 0
        
        titleLabel.alpha = 0
        
    }
    
    //  添加动画
    private func addAnimation(){
        
        let offset:CGFloat = -(ScreenHeight - button - bottom)
        
        imageView.snp.updateConstraints { (make) in
            
            make.bottom.equalTo(view).offset(offset)
        }
        
        UIView.animate(withDuration: 2.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 2, options: [], animations: {
            
            self.view.layoutIfNeeded()
            
        }) { (res) in
            
            UIView.animate(withDuration: 2.0, animations: {
                
                self.titleLabel.alpha = 1;
                
            }, completion: { (res) in
                
               NotificationCenter.default.post(name: NSNotification.Name(ChangeVC), object: changVC.mainTab)
                
            })

        }
    }
    
    //  重写方法
    override func viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
        
        self.addAnimation()
    }
    
    let imageView = UIImageView()
    
    let titleLabel = UILabel(title: "欢迎回来", fontColor: UIColor.lightGray, fontSize: 14)
    
    

}
